﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using TraceTool;

namespace RGPWEB.admin.MCode
{
    public partial class FinalMCode : System.Web.UI.Page
    {
      //  private DataTable dt = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = null;
                dt = new DataTable();
                GetMenuToDataTable(SqlDataSource1.SelectCommand, dt);
                //SqlDataSource1.SelectParameters.Clear();
                //SqlDataSource1.SelectParameters.Add("FDeleted", "1");
                //SqlDataSource1.SelectParameters.Add("FLevel", "3");
                BindTree(dt, null, "0");
            }

        }

        private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID)
        {
           try
           {
             //  TTrace.Debug.Send(string.Format("FParentID={0}", parentID));
               DataRow[] rows = dtSource.Select(string.Format("FParentID={0}", parentID));
            
            foreach (DataRow row in rows)
            {

                TreeNode node = new TreeNode();
                node.Text = row["FName"].ToString();
                node.Value = row["FNumber"].ToString();

               // if (node.Text == "") node.Text = "-----";
               // if (node.Value == "") node.Value = "1111";
               // TTrace.Debug.Send(node.Text).Send(node.Value);
                BindTree(dtSource, node, row["FItemID"].ToString());
                if (parentNode == null)
                {
                   MenuTreeView.Nodes.Add(node);
                }
                else
                {
                    parentNode.ChildNodes.Add(node);
                }

                
            }
           }
           catch (System.Exception ex)
           {
               TTrace.Warning.Send("FinalMCode-BindTree-递归异常");
           }
        }

        private DataTable GetMenuToDataTable(string query, DataTable dt)
        {
          //  using (SqlConnection conn = new SqlConnection(ConnString))
            using (SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);

             //   SqlDataAdapter ada = new SqlDataAdapter(SqlDataSource1.SelectCommand, SqlDataSource1.ConnectionString);
                ada.SelectCommand.Parameters.Add("@FDeleted", SqlDbType.SmallInt,4).Value = 1;//20改成你的长度
                ada.SelectCommand.Parameters.Add("@FLevel", SqlDbType.SmallInt,4).Value = 3;//20改成你的长度
                ada.Fill(dt);
            }
            return dt;
        }

        protected void MenuTreeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            //
            GridView1.DataSourceID = ObjectDataSource1.ID;
        }

        protected void ddlFName_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView1.DataSourceID = ObjectDataSource2.ID;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
